package dRegAut.opg1;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Michael B. Mogensen
 * @see Martin Figure 2.17 (56)
 * 
 * for %%a in (*.dot) do dot -Tpng %%a -o %%a.pngfor %%a in (*.dot) do dot -Tpng %%a -o %%a.png
 */
public class SmallTesting {
    public static void main(String[] args) {
        FA l1 = new SmallTesting().setupL1();
		FA l2 = new SmallTesting().setupL2();
		l1.checkWellDefined();
		l2.checkWellDefined();

		FA unionFA = l1.union(l2);
        FA intersectionFA = l1.intersection(l2);
        FA minusFA = l1.minus(l2);

        unionFA.checkWellDefined();
        intersectionFA.checkWellDefined();
        minusFA.checkWellDefined();

        try {
        	createToDotFile(l1.toDot(), "d:/temp/FAs/L1.dot");
        	createToDotFile(l2.toDot(), "d:/temp/FAs/L2.dot");
            createToDotFile(unionFA.toDot(), "d:/temp/FAs/union.dot");
            createToDotFile(intersectionFA.toDot(), "d:/temp/FAs/intersection.dot");
            createToDotFile(unionFA.toDot(), "d:/temp/FAs/minus.dot");
        } catch (IOException e) {
            e.printStackTrace();
        }

        /*
        System.out.println("Accept 1101 " + unionFA.accepts("1101"));
        System.out.println("Accept 101001 " + unionFA.accepts("101001"));

        System.out.println("Accept empty string " + unionFA.accepts(""));
        System.out.println("Accept 1010 " + unionFA.accepts("1010"));
		*/
    }

    private FA setupL1() {
        FA l1FA = new FA();
        State stateA = new State("A");
        State stateB = new State("B");
        State stateC = new State("C");

        l1FA.alphabet = new Alphabet("ab");

        l1FA.states.add(stateA);
        l1FA.states.add(stateB);
        l1FA.states.add(stateC);

        // Set up graph L1, initial state=a, accepted states=a, b
        l1FA.initial = stateA;
        l1FA.accept.add(stateA);
        l1FA.accept.add(stateB);

        l1FA.setTransition(stateA, 'a', stateB);
        l1FA.setTransition(stateA, 'b', stateA);

        l1FA.setTransition(stateB, 'a', stateC);
        l1FA.setTransition(stateB, 'b', stateA);

        l1FA.setTransition(stateC, 'a', stateC);
        l1FA.setTransition(stateC, 'b', stateC);

        return l1FA;
    }

    private FA setupL2() {
        FA l2FA = new FA();
        State stateP = new State("P");
        State stateQ = new State("Q");
        State stateR = new State("R");

        l2FA.alphabet = new Alphabet("ab");

        l2FA.states.add(stateP);
        l2FA.states.add(stateQ);
        l2FA.states.add(stateR);

        // Set up graph L2, initial state=a, accepted states=c
        l2FA.initial = stateP;
        l2FA.accept.add(stateR);

        l2FA.setTransition(stateP, 'a', stateQ);
        l2FA.setTransition(stateP, 'b', stateP);

        l2FA.setTransition(stateQ, 'a', stateQ);
        l2FA.setTransition(stateQ, 'b', stateR);

        l2FA.setTransition(stateR, 'a', stateQ);
        l2FA.setTransition(stateR, 'b', stateP);

        return l2FA;
    }

    public static void createToDotFile(String toDot, String file) throws IOException {
        FileWriter out = new FileWriter(file);
        out.write(toDot);
        out.close();
    }
}